package com.test04_数组;

import org.junit.Test;

/**
 * 二维数组
 */
public class YootkDemo_03 {

    @Test
    public void test1() {
        int data[][] = new int[][]{{1, 3, 5}, {2, 4, 6, 8}, {9, 10}};
        for (int x = 0; x < data.length; x++) {
            for (int y = 0; y < data[x].length; y++) {
                System.out.print("data[" + x + "][" + y + "] = " + data[x][y] + "\t\t");
            }
            System.out.println();
        }
    }

    @Test
    public void test2() {
        int data[][] = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        ArrayUtil util = new ArrayUtil(data);
        System.out.println("数组转置前：");
        util.printArray();
        System.out.println("数组转置后：");
        util.reverse();
        util.printArray();
    }
}

class ArrayUtil {
    private int array[][];      // 二维数组
    private boolean finish;     // 防止重复的操作

    public ArrayUtil(int[][] array) {
        this.array = array;
    }

    // 数组转置处理
    public void reverse() {
        if (!this.finish) {
            for (int x = 0; x < this.array.length; x++) {
                for (int y = 0; y < x; y++) {
                    if (x != y) {
                        int temp = this.array[x][y];
                        this.array[x][y] = this.array[y][x];
                        this.array[y][x] = temp;
                    }
                }
            }
            this.finish = true;
        }
    }

    // 打印数组内容
    public void printArray() {
        for (int x = 0; x < this.array.length; x++) {
            for (int y = 0; y < this.array[x].length; y++) {
                System.out.print(this.array[x][y] + "\t\t");
            }
            System.out.println();
        }
    }
}
